package kr.hkit.powerjava.chther14.windowbulider;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

public class CalculatorEx extends JFrame implements ActionListener {

	private JPanel contentPane;
	private CalPanel panel;
	private JPanel panel_1;
	private JButton btnclear;
	private JButton btncal;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CalculatorEx frame = new CalculatorEx();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CalculatorEx() {
		initialize();
	}
	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		panel = new CalPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		
		panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);
		
		btncal = new JButton("계산");
		btncal.addActionListener(this);
		panel_1.add(btncal);
		
		btnclear = new JButton("clear");
		btnclear.addActionListener(this);
		panel_1.add(btnclear);
		pack();
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("계산")){
//			System.out.printf("%s*%s",panel.getTextField(),panel.getTextField_1());
			try{
			int left = Integer.parseInt(panel.getTextField().trim());
			int right = Integer.parseInt(panel.getTextField_1().trim());
			panel.setTextField_2(left*right+"");
			}catch(NumberFormatException e1){
				JOptionPane.showMessageDialog(this, "숫자만");
			}
		}else{
			panel.setTextField("");
			panel.setTextField_1("");
			panel.setTextField_2("");
//			panel.repaint();
		}
	}
}
